package dz.salah.ouchenne.com;
import org.apache.ws.policy.Policy;
import org.apache.ws.policy.util.PolicyReader;
import org.apache.ws.policy.util.PolicyWriter;
import org.apache.ws.policy.util.PolicyFactory;
import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.Vector;

class PolicyExample{
public static
void main(String argsv[])
{
Policy Requierements = readPolicy("C:/Users/huhu/Desktop/code/Request1.xml");
Policy Offers = readPolicy("C:/Users/huhu/Desktop/code/Offers1.xml");
System.out.printf ("With Syntax Matching, the Final Score is = %3.1f%n",MatchingSyntaxically(Requierements,Offers));
System.out.printf ("With Semantic Matching, the Final Score is= %3.1f%n",MatchingSemantically(Requierements,Offers));
}


public static Policy readPolicy(String file){
Policy policy = null;
try
{
  PolicyReader reader = PolicyFactory.getPolicyReader (PolicyFactory.DOM_POLICY_READER);
  FileInputStream fis = new FileInputStream(file);
  policy = reader.readPolicy(fis);
}
catch(Exception e) 
{
  System.out.println("Exception occurred: " + e);
  e.printStackTrace();
}
return policy;
}

static double MatchingSyntaxically(Policy R, Policy O)
{
	Requirements Requirements= new Requirements(R);
	Offers  Offers=new Offers(O);
	
	double Score=0;

int Max=0;
for(int i=0;i<Requirements.getEncryption().size();i++)
   {
	
	 if((Offers.getEncryption().contains(Requirements.getEncryption().elementAt(i).getQName()))
			 &&(Requirements.getEncryption().elementAt(i).getPreference()>Max))
	 Max=Requirements.getEncryption().elementAt(i).getPreference(); 
	
   }
Score+=Max*Requirements.ImportanceEncryption;

Max=0;
for(int i=0;i<Requirements.getDigitalSignature().size();i++)
   {
	
	 if((Offers.getDigitalSignature().contains(Requirements.getDigitalSignature().elementAt(i).getQName()))
			 &&(Requirements.getDigitalSignature().elementAt(i).getPreference()>Max))
	 Max=Requirements.getDigitalSignature().elementAt(i).getPreference(); 
	
   }
Score+=Max*Requirements.ImportanceDigitalSignature;


Max=0;
for(int i=0;i<Requirements.getProtocol().size();i++)
   {
	
	 if((Offers.getProtocol().contains(Requirements.getProtocol().elementAt(i).getQName()))
			 &&(Requirements.getProtocol().elementAt(i).getPreference()>Max))
	 Max=Requirements.getProtocol().elementAt(i).getPreference(); 
	
   }
Score+=Max*Requirements.ImportanceProtocol;

return Score;	
}


static double MatchingSemantically(Policy R, Policy O)
{
	Requirements Requirements= new Requirements(R);
	Offers  Offers=new Offers(O);
	double Score=0;

int Max=0;
for(int i=0;i<Requirements.getEncryption().size();i++)
   {
	
	 if(
	(Match(Offers.getEncryption(),Requirements.getEncryption().elementAt(i).getQName()) )
			 
			 &&(Requirements.getEncryption().elementAt(i).getPreference()>Max))
	 Max=Requirements.getEncryption().elementAt(i).getPreference(); 
	
   }
Score+=Max*Requirements.ImportanceEncryption;

Max=0;
for(int i=0;i<Requirements.getDigitalSignature().size();i++)
   {
	
	 if((Offers.getDigitalSignature().contains(Requirements.getDigitalSignature().elementAt(i).getQName()))
			 &&(Requirements.getDigitalSignature().elementAt(i).getPreference()>Max))
	 Max=Requirements.getDigitalSignature().elementAt(i).getPreference(); 
	
   }
Score+=Max*Requirements.ImportanceDigitalSignature;


Max=0;
for(int i=0;i<Requirements.getProtocol().size();i++)
   {
	
	 if((Offers.getProtocol().contains(Requirements.getProtocol().elementAt(i).getQName()))
			 &&(Requirements.getProtocol().elementAt(i).getPreference()>Max))
	 Max=Requirements.getProtocol().elementAt(i).getPreference(); 
	
   }
Score+=Max*Requirements.ImportanceProtocol;

return Score;	
}

static boolean Match(Vector<String> Offers, String Element)
{
	
	//Load the Ontology From the File Path Specified below
	String File="C:/Users/huhu/Desktop/ontologies/Ontology1335169020304/Security.owl";
	OWLOntologyManager OntologyManager;
	OWLOntology Ontology = null;
	Reasoner REASONER;
	OWLDataFactory Factory ;
	IRI OntologyIRI = null;
	OntologyManager=OWLManager.createOWLOntologyManager();
	Factory = OntologyManager.getOWLDataFactory();
	try {
			Ontology=OntologyManager.loadOntologyFromOntologyDocument(new File(File));
			OntologyIRI=Ontology.getOntologyID().getOntologyIRI();	
		} 
	catch (OWLOntologyCreationException e) 
	    {
			
		e.printStackTrace();
		}
		REASONER=new Reasoner(Ontology);
if(Offers.contains(Element)) return true;

for(int i=0;i<Offers.size();i++)
{
		
	IRI ElementIRI=IRI.create(OntologyIRI.toString()+"#"+Element);
	OWLIndividual individual=Factory.getOWLNamedIndividual(ElementIRI);
	
	Set<OWLClassAssertionAxiom> L=	Ontology.getClassAssertionAxioms(individual);
	ArrayList<OWLClassAssertionAxiom> list = new ArrayList( L );
	OWLClassAssertionAxiom L1=(OWLClassAssertionAxiom)list.get(0);
    String S1=
    		L1.getAxiomWithoutAnnotations().toString().substring(0,L1.getAxiomWithoutAnnotations().toString().indexOf('>'));
	

	ElementIRI=IRI.create(OntologyIRI.toString()+"#"+Offers.elementAt(i));
	individual=Factory.getOWLNamedIndividual(ElementIRI);
	L=	Ontology.getClassAssertionAxioms(individual);
	list = new ArrayList( L );
	OWLClassAssertionAxiom L2=(OWLClassAssertionAxiom)list.get(0);
    
	String S2=
    		L2.getAxiomWithoutAnnotations().toString().substring(0,L2.getAxiomWithoutAnnotations().toString().indexOf('>'));
	
	
	if(S1.equals(S2))return true;
	
	
}	





return false;
}
public static void writePolicy(Policy w, String file)
{
try{
     FileOutputStream fos = new FileOutputStream(file);
     PolicyWriter writer = PolicyFactory.getPolicyWriter (PolicyFactory.StAX_POLICY_WRITER);
     writer.writePolicy(w, fos);
   }
catch(Exception e) 
   {
     System.out.println("Exception occurred: " + e);
     e.printStackTrace();
   }
}


}